package com.yonyou.iuap.activiti.demo.test;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.junit.Test;

import java.io.*;
import java.util.List;
import java.util.zip.ZipInputStream;

public class ActivitiDemo {
    /**
     * 流程部署方式
     * 1. 根据 bpm+png方式部署
     * 2. 根据输入流方式部署
     * 3. 根据输入流+zip文件方式部署
     */
    @Test
    public void deployFromClassPath(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getRepositoryService().createDeployment()
                .addClasspathResource("test.bpmn")
                .addClasspathResource("test.png")
                .deploy();
    }

    @Test
    public void deployFromInputStream(){
        InputStream is = this.getClass().getClassLoader().getResourceAsStream("qingjia.bpmn");
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getRepositoryService().createDeployment()
                .addInputStream("qingjia.bpmn",is)
                .deploy();
    }

    @Test
    public void delopyFromZipInputstream(){
        InputStream is = this.getClass().getClassLoader().getResourceAsStream("test.zip");
        ZipInputStream zipInputStream = new ZipInputStream(is);
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getRepositoryService().createDeployment()
                .addZipInputStream(zipInputStream)
                .deploy();
    }

    /**
     * 删除流程
     */
    @Test
    public void testDelete(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //第一个参数是部署的流程的ID,act_re_deployment 表的id ;第二个参数 true 表示进行级联删除.
        processEngine.getRepositoryService().deleteDeployment("1",true);
    }

    /**
     * 查询流程部署
     */
    @Test
    public void queryDeploymentByName(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        List<Deployment> deploymentList = processEngine.getRepositoryService().createDeploymentQuery()
                .orderByDeploymenTime()
                .desc()
                //.deploymentName("test")
                .list();
        for (Deployment deployment : deploymentList) {
            System.out.println(deployment.getId() + "部署的名称为" + deployment.getName());
        }
    }

    /**
     * 查询所有的流程定义
     */
    @Test
    public void queryAllDP(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        List<ProcessDefinition> processDefinitions = processEngine.getRepositoryService().createProcessDefinitionQuery()
                .orderByProcessDefinitionVersion()
                .desc()
                .list();
        for (ProcessDefinition processDefinition : processDefinitions) {
            System.out.println(processDefinition.getName());
        }
    }

    /**
     * 查看流程图
     * @throws IOException
     */
    @Test
    public void testShowImage() throws IOException {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //12501 test.png 分别位于 act_re_deployment 和act_ge_bytearray 表中
        InputStream is = processEngine.getRepositoryService().getResourceAsStream("12501","test.png");
        OutputStream os = new FileOutputStream("E:/test001.png");
        int b = -1;
        while((b=is.read()) != -1){
            os.write(b);
        }
        is.close();
        os.close();
    }

    /**
     * 根据pid查看流程图
     * @throws IOException
     */
    @Test
    public void testshowImageBypid() throws IOException {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //shenqing:1:12504 从act_re_procdef 中获取
        InputStream is = processEngine.getRepositoryService().getProcessDiagram("shenqing:1:12504");
        OutputStream os = new FileOutputStream("E:/test002.png");
        int b = -1;
        while( (b = is.read()) != -1){
            os.write(b);
        }
        is.close();
        os.close();
    }

    /**
     * 查看bpmn文件
     * @throws IOException
     */
    @Test
    public void testshowbpm() throws IOException {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        InputStream is = processEngine.getRepositoryService().getProcessModel("shenqing:1:12504");
        OutputStream os = new FileOutputStream("E:/test.bpmn");
        int b = -1;
        while( (b = is.read()) != -1){
            os.write(b);
        }
        is.close();
        os.close();
    }
}
